package com.lanhe.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.product.domain.LhKayuProduct;
import com.lanhe.product.domain.LhKayuProductDetails;
import com.lanhe.product.mapper.LhKayuProductMapper;
import com.lanhe.product.service.ILhKayuProductContentService;
import com.lanhe.product.service.ILhKayuProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 卡鱼产品表(LhKayuProduct)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-22 18:01:05
 */
@Service
public class LhKayuProductServiceImpl extends ServiceImpl<LhKayuProductMapper, LhKayuProduct> implements ILhKayuProductService {

	@Autowired
	private LhKayuProductMapper productMapper;

	@Autowired
	private ILhKayuProductContentService contentService;

	/**
	 * 列表查询
	 *
	 * @param product 实体
	 * @return java.util.List<com.lanhe.article.domain.LhArticle>
	 * @author zhanggl
	 * @date 2023/12/22
	 */
	@Override
	public List<LhKayuProduct> selectKayuProductList(LhKayuProduct product) {
		LambdaQueryWrapper<LhKayuProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(product.getProductName()), LhKayuProduct::getProductName,product.getProductName());
		queryWrapper.ge(ObjectUtil.isNotNull(product.getBeginTime()), LhKayuProduct::getCreateTime, product.getBeginTime());
		queryWrapper.le(ObjectUtil.isNotNull(product.getEndTime()), LhKayuProduct::getCreateTime, product.getEndTime());
		queryWrapper.eq(LhKayuProduct::getDelFlag, Constants.GeneralIntType.ZERO);
		queryWrapper.orderByDesc(LhKayuProduct::getProductId);
		return productMapper.selectList(queryWrapper);
	}

	/**
	 * 根据ID查询
	 *
	 * @param productId 产品ID
	 * @return com.lanhe.article.domain.LhArticle
	 * @author zhanggl
	 * @date 2023/12/22
	 */
	@Override
	public LhKayuProduct selectKayuProductById(Long productId) {

		LambdaQueryWrapper<LhKayuProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhKayuProduct::getDelFlag, 0)
				.eq(LhKayuProduct::getProductId, productId);
		LhKayuProduct kayuProduct = productMapper.selectOne(queryWrapper);
		LhKayuProductDetails productContent = contentService.selectKayuProductContentByProductId(productId);
		if (ObjectUtil.isNotNull(productContent)){
			kayuProduct.setContent(productContent.getContent());
		}
		return kayuProduct;
	}

	/**
	 * 新增
	 *
	 * @param product 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/22
	 */
	@Override
	public int insertKayuProduct(LhKayuProduct product) {
		int insertNum = productMapper.insert(product);
		contentService.insertKayuProductContent(product.getProductId(),product.getContent());
		return insertNum;
	}

	/**
	 * 修改
	 *
	 * @param product 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/22
	 */
	@Override
	public int updateKayuProduct(LhKayuProduct product) {
		LambdaQueryWrapper<LhKayuProduct> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhKayuProduct::getDelFlag, 0)
				.eq(LhKayuProduct::getProductId, product.getProductId());
		int updateNum = productMapper.update(product, queryWrapper);
		contentService.updateKayuProductContent(product.getProductId(),product.getContent());
		return updateNum;
	}

	/**
	 * 删除
	 *
	 * @param productIds 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/22
	 */
	@Override
	public int deleteKayuProductByIds(Long[] productIds) {
		LambdaUpdateWrapper<LhKayuProduct> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(LhKayuProduct::getDelFlag, Constants.GeneralIntType.TWO+"")
				.in(LhKayuProduct::getProductId,productIds);
		return productMapper.update(null,updateWrapper);
	}
}


